package com.howtoplay.Handler;
import Util.ArrayUtils;
import com.anoyi.grpc.util.ProtobufUtils;
import com.howtoplay.Message.BaseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.BinaryWebSocketHandler;

public class WebsocketBinaryHandler extends BinaryWebSocketHandler {
    private static final Logger log = LoggerFactory.getLogger(WebsocketBinaryHandler.class);
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("----------连接开启------------");
        super.afterConnectionEstablished(session);
    }

    @Override
    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
        byte[] bytes = new byte[message.getPayloadLength()];
        System.out.println("bytes-->"+ ArrayUtils.byteArrayToString(bytes));
        message.getPayload().get(bytes,0,bytes.length);
        BaseMessage msg = ProtobufUtils.deserialize(bytes,BaseMessage.class);
        super.handleBinaryMessage(session, message);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("----------连接断开------------");
        System.out.println(status.toString());
        super.afterConnectionClosed(session,status);
    }




}
